package services

import (
	"fmt"
	"lottery_code/dao"
	"lottery_code/datasource"
	"lottery_code/models"
	"strconv"
	"time"
)

type UserDayService interface {
	Get(id int) *models.LtUserday
	GetAll(page, size int) []models.LtUserday
	CountAll() int64
	Search(uid, day int) []models.LtUserday
	Count(uid, day int) int
	Update(data *models.LtUserday, columns []string) error
	Create(data *models.LtUserday) error
	GetUserToday(uid int) *models.LtUserday
}

type userDay struct {
	dao *dao.UserdayDao
}

func NewUserDayService() UserDayService {
	return &userDay{
		dao: dao.NewUserdayDao(datasource.NewDbMater()),
	}
}

func (ud *userDay) Get(id int) *models.LtUserday {
	return ud.dao.Get(id)
}
func (ud *userDay) GetAll(page, size int) []models.LtUserday {
	return ud.dao.GetAll(page, size)
}
func (ud *userDay) CountAll() int64 {
	return ud.dao.CountAll()
}
func (ud *userDay) Search(uid, day int) []models.LtUserday {
	return ud.dao.Search(uid, day)
}
func (ud *userDay) Count(uid, day int) int {
	return ud.dao.Count(uid, day)
}
func (ud *userDay) Update(data *models.LtUserday, columns []string) error {
	return ud.dao.Update(data, columns)
}
func (ud *userDay) Create(data *models.LtUserday) error {
	return ud.dao.Create(data)
}
func (s *userDay) GetUserToday(uid int) *models.LtUserday {
	y, m, d := time.Now().Date()
	strDay := fmt.Sprintf("%d%02d%02d", y, m, d)
	day, _ := strconv.Atoi(strDay)
	list := s.dao.Search(uid, day)
	if list != nil && len(list) > 0 {
		return &list[0]
	} else {
		return nil
	}
}